package edu.iyte.ceng431.hw2.pos.controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import edu.iyte.ceng431.hw2.pos.db.Search;
import edu.iyte.ceng431.hw2.pos.view.BarChartView;
import edu.iyte.ceng431.hw2.pos.view.MainFrame;

public class ReportController {
	private MainFrame mainView;
	private BarChartView barChartView;
	public ReportController(MainFrame mainView) {
		super();
		this.mainView = mainView;
		this.mainView.addGetReportListener(new ReportListener());

	}
	class ReportListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			
			String dt1 = mainView.getFromDate() + " 00:00:00";
			String dt2 = mainView.getToDate() + " 00:00:00";
			ArrayList<Double> totals = getTotals(dt1, dt2);
			int size = totals.size();
			if (size == 0)
				JOptionPane.showMessageDialog(mainView, "No Report.");
			else {
				List<Double> cashTotals = totals.subList(0, size / 2);
				List<Double> cardTotals = totals.subList((size / 2), size);
				barChartView = new BarChartView();
				barChartView.setCashTotals(cashTotals);
				barChartView.setCardTotals(cardTotals);
				barChartView.setDt1(dt1);
				barChartView.setDt2(dt2);
				JFrame frame = new JFrame();
				frame.setTitle("Sales Reports");
				frame.setSize(1000, 300);
				Container contentPane = frame.getContentPane();
				contentPane.add(barChartView);
				barChartView.repaint();
				frame.setVisible(true);
			}
		}
	}

	public ArrayList<Double> getTotals(String dt1, String dt2) {

		java.util.Date lastDate = null, earlyDate = null, history = null;
		int MILLSECS_PER_DAY = 24 * 60 * 60 * 1000;
		int deltaDays = 0;
		ArrayList<Double> totals = new ArrayList<Double>();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			earlyDate = sdf.parse(dt1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			lastDate = sdf.parse(dt2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		history = earlyDate;
		deltaDays = (int) ((lastDate.getTime() - earlyDate.getTime()) / MILLSECS_PER_DAY);

		if (deltaDays <= 7) {
			for (int i = 0; i < deltaDays; i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 1);
				double total = Search.getTotalSale(history, c.getTime(), 1);
				totals.add(total);
				history = c.getTime();
			}
			history = earlyDate;
			for (int i = 0; i < deltaDays; i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 1);
				double total = Search.getTotalSale(history, c.getTime(), 2);
				totals.add(total);
				history = c.getTime();
			}
		}

		else if (deltaDays > 7 && deltaDays <= 30) {
			for (int i = 0; i <= (deltaDays / 7); i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 7);
				double total = Search.getTotalSale(history, c.getTime(), 1);
				totals.add(total);
				history = c.getTime();
			}
			history = earlyDate;
			for (int i = 0; i <= (deltaDays / 7); i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 7);
				double total = Search.getTotalSale(history, c.getTime(), 2);
				totals.add(total);
				history = c.getTime();
			}
		} else if (deltaDays > 30) {
			for (int i = 0; i < (deltaDays / 30); i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 30);
				double total = Search.getTotalSale(history, c.getTime(), 1);
				totals.add(total);
				history = c.getTime();
			}
			history = earlyDate;
			for (int i = 0; i < (deltaDays / 30); i++) {
				Calendar c = Calendar.getInstance();
				c.setTime(history);
				c.add(Calendar.DATE, 30);
				double total = Search.getTotalSale(history, c.getTime(), 2);
				totals.add(total);
				history = c.getTime();
			}
		}

		return totals;
	}

}
